/*********************
 * Paul Levandoski
 * Project One
 * Main Process
 **************/

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#define READ 0
#define WRITE 1
#define BIGINT 128
#define NUMPARENT 1 //assuming 2 parent processes
#define FILESPERC 5 //assuming 5 files per child

//Global Variables
int pip[NUMPARENT][2];

int main()
{
    //Variables based on the 
    //    number of parents needed
    pid_t pid[NUMPARENT];
    int nextNum[NUMPARENT][2];
    int allNumInOrder[NUMPARENT*FILESPERC*2];
    int i = 0;
    int j = 0;
    char* prc = "./mupesP";
    
    //Sets 
    while(i < NUMPARENT){
	    nextNum[i][0] = 1000;
	    nextNum[i][1] = 0;
	    //printf("First: %d \t Second: %d\n", nextNum[i][0], nextNum[i][1]);
	    i++;
    }
    
    //Creates plumbing 
    i = 0;
    while(i < NUMPARENT){
	    if(pipe(pip[i]) < 0){
		    perror("plumbing problem ");
		    exit(1);
	    }
	    printf("pipe %d made\n", i);
	    i++;
    }
    
    
    //Creates parent processes
    i = 0;
    while(i < NUMPARENT){
	    if((pid[i] = fork()) < 0) {
		    perror("fork failed");
		    exit(1);
	    }else if(!pid[i]){   //New parent setup
		    printf("I the new parent -exec off\n");
		    //out of date
		    char* const buf = '\0';
		    
		    //sets up pipes in the right direction
		    dup2(pip[i][READ], STDIN_FILENO);
		    dup2(pip[i][WRITE], STDOUT_FILENO);
		    execvp(prc, &buf);
            perror("Execution has failed");
		    exit(1);
	    }
	    i++;
    }
    
    sleep(1);
    //Tells the parents their number in the series
    i = 1;
    while(i < NUMPARENT + 1){
	    write(pip[i][WRITE], (const void *)&i, sizeof(BIGINT));
	    printf("write to parent %d\n", i);
	    i++;
    }
    
    // An old test to check the numbers passed to the parents
    i = 0;
    int numRec;
    while(i < NUMPARENT){
	    numRec = read(pip[i][READ], &numRec, sizeof(BIGINT));
	    printf("Read from parent %d\n", numRec);
	    i++;
    }
    
    
    //Main loop
    i = 0;
    j = 0;
	int numTempLow = 0;
	int tempJ = 0;
    while(i < (NUMPARENT * FILESPERC * 2)){
	    //get new values from the parents
	    j=0;
	    while(j < NUMPARENT){
		    printf("First: %d \t Second: %d\n", nextNum[j][0], nextNum[j][1]);
		    if(nextNum[j][0] == 1000 && nextNum[j][1] < (2*FILESPERC)){
			    printf("gonna read now\n");
			    read (pip[j][READ], &nextNum[j][0], sizeof(BIGINT));
			    nextNum[j][1] = nextNum[j][1] + 1;
		    }
		    printf("First: %d \t Second: %d\n", nextNum[j][0], nextNum[j][1]);
		    j++;
	    }
	    
	    numTempLow = nextNum[0][0];
	    tempJ = 0;
	    j = 1;
	    while(j < NUMPARENT){
		    if(nextNum[j][0] < numTempLow){
			    numTempLow = nextNum[j][0];
			    tempJ = j;
		    }
		    j++;
	    }
	    
	    allNumInOrder[i] = nextNum[tempJ][0];
	    nextNum[tempJ][0] = 1000;
	    i++;
    } 
    
    i = 0;
    while(i < (NUMPARENT * FILESPERC * 2)){
	    printf("Number: %d\n", allNumInOrder[i++]);
    }
    
    printf("Added a print statment\n");
    return 0;
}